package controllers;

import com.fasterxml.jackson.databind.JsonNode;
import dao.DeliveryPlan_Repository;
import org.w3c.dom.Document;
import play.data.FormFactory;
import play.libs.Json;
import play.libs.concurrent.HttpExecutionContext;
import play.mvc.Controller;
import play.mvc.Result;

import javax.inject.Inject;
import java.util.concurrent.CompletionStage;

public class DeliveryPlanController extends Controller{

    private final FormFactory formFactory;
    private final DeliveryPlan_Repository tDeliveryPlanRepository;
    private final HttpExecutionContext ec;

    @Inject
    public DeliveryPlanController(FormFactory formFactory, DeliveryPlan_Repository tDeliveryPlanRepository, HttpExecutionContext ec) {
        this.formFactory = formFactory;
        this.tDeliveryPlanRepository = tDeliveryPlanRepository;
        this.ec = ec;
    }

    public CompletionStage<Result> getDeliPlanList() {
        JsonNode json = request().body().asJson();
        String BillNo = json.findPath("BillNo").textValue();

        return tDeliveryPlanRepository.getDeliPlanList(BillNo).thenApplyAsync(_GetListByProc -> {
            return ok(Json.toJson(_GetListByProc));
        }, ec.current());
    }

    public CompletionStage<Result> createDeliPlan(){
        JsonNode json = request().body().asJson();
        String PhoneNumber = json.findPath("PhoneNumber").textValue();
        String LoadingSubNum = json.findPath("LoadingSubNum").textValue();
        String DeliveryPlace = json.findPath("DeliveryPlace").textValue();
        String WeigherCode = json.findPath("WeigherCode").textValue();
        String Amount = json.findPath("Amount").textValue();
        String StartDate = json.findPath("StartDate").textValue();
        String EndDate = json.findPath("EndDate").textValue();
        String TransType = json.findPath("TransType").textValue();
        String ShipName = json.findPath("ShipName").textValue();
        String UnloadPlace = json.findPath("UnloadPlace").textValue();
        String CustomerDept = json.findPath("CustomerDept").textValue();
        String ConUser = json.findPath("ConUser").textValue();
        String BatchCode = json.findPath("BatchCode").textValue();
        String DeliveryNo = json.findPath("DeliveryNo").textValue();

        return tDeliveryPlanRepository.createDeliPlan(PhoneNumber,LoadingSubNum, DeliveryPlace
                , WeigherCode, Amount, StartDate, EndDate , TransType , ShipName
                , UnloadPlace, CustomerDept, ConUser, BatchCode, DeliveryNo).thenApplyAsync(_GetListByProc -> {
            return ok(Json.toJson(_GetListByProc));
        }, ec.current());
    }

    public CompletionStage<Result> cancleDeliPlan(){
        JsonNode json = request().body().asJson();
        String PhoneNumber = json.findPath("PhoneNumber").textValue();
        String StartDate = json.findPath("StartDate").textValue();
        String EndDate = json.findPath("EndDate").textValue();
        String DeliveryNo = json.findPath("DeliveryNo").textValue();
        String LoadingSubNum = json.findPath("LoadingSubNum").textValue();

        return tDeliveryPlanRepository.cancleDeliPlan(PhoneNumber, StartDate, EndDate, DeliveryNo, LoadingSubNum).thenApplyAsync(_GetListByProc -> {
            return ok(Json.toJson(_GetListByProc));
        }, ec.current());
    }

    public CompletionStage<Result> editDeliPlan(){
        JsonNode json = request().body().asJson();
        String PhoneNumber = json.findPath("PhoneNumber").textValue();
        String Amount = json.findPath("Amount").textValue();
        String StartDate = json.findPath("StartDate").textValue();
        String EndDate = json.findPath("EndDate").textValue();
        String DeliveryNo = json.findPath("DeliveryNo").textValue();
        String LoadingSubNum = json.findPath("LoadingSubNum").textValue();

        return tDeliveryPlanRepository.editDeliPlan(PhoneNumber, Amount, StartDate, EndDate, DeliveryNo, LoadingSubNum).thenApplyAsync(_GetListByProc -> {
            return ok(Json.toJson(_GetListByProc));
        }, ec.current());
    }

    public CompletionStage<Result> getDeliveryPlan() {
        Document doc_getDeliveryPlan = request().body().asXml();

        return tDeliveryPlanRepository.getDeliveryPlan().thenApplyAsync(_getRoutes -> {
            return ok(_getRoutes.getXmlString()).as("application/xml");
        }, ec.current());
    }

    public CompletionStage<Result> createDeliveryPlan(){
        JsonNode json = request().body().asJson();
        String PhoneNumber = json.findPath("PhoneNumber").textValue();
        String LoadingSubNum = json.findPath("LoadingSubNum").textValue();
        String DeliveryPlace = json.findPath("DeliveryPlace").textValue();
        String WeigherCode = json.findPath("WeigherCode").textValue();
        String Amount = json.findPath("Amount").textValue();
        String StartDate = json.findPath("StartDate").textValue();
        String EndDate = json.findPath("EndDate").textValue();
        String TransType = json.findPath("TransType").textValue();
        String ShipName = json.findPath("ShipName").textValue();
        String UnloadPlace = json.findPath("UnloadPlace").textValue();
        String CustomerDept = json.findPath("CustomerDept").textValue();
        String ConUser = json.findPath("ConUser").textValue();
        String BatchCode = json.findPath("BatchCode").textValue();
        String DeliveryNo = json.findPath("DeliveryNo").textValue();
        String DeliveryOrderNo = json.findPath("DeliveryOrderNo").textValue();

        return tDeliveryPlanRepository.createDeliveryPlan(PhoneNumber,LoadingSubNum, DeliveryPlace
                , WeigherCode, Amount, StartDate, EndDate , TransType , ShipName
                , UnloadPlace, CustomerDept, ConUser, BatchCode, DeliveryNo,DeliveryOrderNo).thenApplyAsync(_GetListByProc -> {
            return ok(Json.toJson(_GetListByProc));
        }, ec.current());
    }

    public CompletionStage<Result> getDeliveryPlanPage(){
        JsonNode json = request().body().asJson();
        Integer RowId = json.findPath("RowId").intValue();
        Integer Length = json.findPath("Length").intValue();


        return tDeliveryPlanRepository.getDeliveryPlanPage(RowId, Length).thenApplyAsync(_GetListByProc -> {
            return ok(_GetListByProc.getXmlString()).as("application/xml");
        }, ec.current());
    }

    public CompletionStage<Result> createDeliveryOrder(){
        JsonNode json = request().body().asJson();
        String PhoneNumber = json.findPath("PhoneNumber").textValue();
        Integer DeliveryPlanNo = json.findPath("DeliveryPlanNo").intValue();
        String DeliveryOrderNo = json.findPath("DeliveryOrderNo").textValue();
        String DeliveryPlace = json.findPath("DeliveryPlace").textValue();
        String WeigherCode = json.findPath("WeigherCode").textValue();
        String Amount = json.findPath("Amount").textValue();
        String StartDate = json.findPath("StartDate").textValue();
        String EndDate = json.findPath("EndDate").textValue();
        String TransType = json.findPath("TransType").textValue();
        String ShipName = json.findPath("ShipName").textValue();
        String UnloadPlace = json.findPath("UnloadPlace").textValue();
        String CustomerDept = json.findPath("CustomerDept").textValue();
        String ConUser = json.findPath("ConUser").textValue();
        String BatchCode = json.findPath("BatchCode").textValue();

        return tDeliveryPlanRepository.createDeliveryOrder(PhoneNumber,DeliveryPlanNo, DeliveryOrderNo, DeliveryPlace
                , WeigherCode, Amount, StartDate, EndDate , TransType , ShipName
                , UnloadPlace, CustomerDept, ConUser, BatchCode).thenApplyAsync(_GetListByProc -> {
            return ok(Json.toJson(_GetListByProc));
        }, ec.current());
    }
}
